package com.hiwan.system.service.dict;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hiwan.system.common.base.GenericService;
import com.hiwan.system.dao.dict.DictDao;
import com.hiwan.system.pojo.DictEntry;
import com.hiwan.system.pojo.DictType;
import com.hiwan.system.util.DictCacheUtils;

@Service
@Transactional
public class DictService extends GenericService<DictDao, DictEntry, String>{
	
	public void insertDictEntry(DictEntry dictEntry) {
		dao.insertDictEntry(dictEntry);
		DictCacheUtils.mergeDictEntry(dictEntry);
	}

	public void updateDictEntry(DictEntry dictEntry) {
		dao.updateDictEntry(dictEntry);
		DictCacheUtils.mergeDictEntry(dictEntry);
	}

	public List<DictEntry> selectDictEntry(String dictId,String dictCode) {
		return dao.selectDictEntry(dictId,dictCode);
	}
	
	public List<DictType> selectDictType(DictType dictType){
		return dao.selectDictTypePage(dictType);
	}
	public void insertDictType(DictType dictType){
		dao.insertDictType(dictType);
	}
	public void deleteEntry(String dictType,String dictCode){
		DictEntry dictEntry = new DictEntry();
		dictEntry.setDictType(dictType);
		dictEntry.setDictCode(dictCode);
		dao.removeDictEntry(dictEntry);
		DictCacheUtils.removeDictEntry(dictEntry);
	}
	public String updateDictType(DictType dictType) {
		String message="fail";
		int temp = dao.updateDictType(dictType);
		if ( temp > 0)
			message="success";
		 return message;
	}
	
	public String checkDictEntry(String dictType){
		List<DictEntry> entryList = dao.selectDictEntry(dictType,null);
		if(entryList == null || entryList.isEmpty()){
			return "success";
		}else{
			return "failure";
		}
		
	}
	
	public int removeDictType(String dictType){
			return dao.removeDictType(dictType);
	}
}
